public class Workspace extends AtomBase {
	
	public Map<String, ServiceCollection> collections { 
		get {
			if (collections == null) {
				collections = new Map<String, ServiceCollection>();
			}
			return collections;
		}
		set; 
	}
	public List<ServiceCollection> collection { 
		get {
			if (collection == null) {
				collection = new List<ServiceCollection>();
			}
			return collection;
		}
		set; 
	}
	
	public List<ServiceCollection> getCollections() {
		return collection;
	}
	
	public ServiceCollection getCollectionByTitle(String title) {
		System.debug('\n\n\t Getting collection by title : ' + title + '\n');
		for (ServiceCollection sc : collection) {
			System.debug('\n\n\t Looping over collection ' + sc.getTitle() + '\n');
			if ( sc.getTitle() != null ) {
				String t1 = title.toLowerCase();
				String t2 = sc.getTitle().toLowerCase();
				System.debug('\n\n\t Comparing passed title: ' + title + '\n\t to :' + sc.getTitle() + '\n');
				if (t2.startsWith(t1)) {
					System.debug('\n\n\t Found the collection and returning it...\n');
					return sc;
				}
			}
		}
		return null; 
	}
	
	public Workspace(XmlStreamReader reader) {
		super(reader);
	}

	public override void handleStartTag(XmlStreamReader reader, String nodeName) {
		System.debug(LoggingLevel.INFO, '\n\nHandling the start tag in the Workspace...\n');
   		System.debug(LoggingLevel.INFO, '\n\n WORKSPACE - start tag: ' + nodeName + '\n');
		String tagName = 'workspace';
		handleAttributes(reader, tagName);
        if (reader.hasNext()) {
			reader.next();
			while (reader.hasNext()) {
				if (reader.getEventType() == XmlTag.END_ELEMENT) {
					System.debug(LoggingLevel.INFO, '\n\n Handleing the end element in workspace \n TagName: ' + reader.getLocalName() + '\n');
					if (reader.getLocalName() == 'workspace') {
						return;
					} 
				} else if (reader.getEventType() == XmlTag.CHARACTERS) {
                	if ( ! reader.isWhitespace()) { 
                		if (tagName == 'title') {
                			setTitle(reader.getText());
                		}
                	}
            	} else if (reader.getEventType() == XmlTag.START_ELEMENT) { 
					tagName = reader.getLocalName(); 
					handleAttributes(reader, tagName);
					if (tagName == 'collection') {
						System.debug(LoggingLevel.INFO, '\n\n WORKSPACE - adding collection..');
						collection.add(new ServiceCollection(reader));
					}
				}
               	reader.next(); 
			}
        } 
	}
		
	private void handleAttributes(XmlStreamReader reader, String tagName) {
		for (integer i=0; i<reader.getAttributeCount(); i++) {
      		String name = reader.getAttributeLocalName(i);
       		String value = reader.getAttributeValueAt(i);
        }
	}

}